import "reflect-metadata";
import {createConnection} from "typeorm";
//定义的实体类
import {User} from "./entity/User";
import {UserExtend} from './entity/UserExtend'

// 增
// createConnection().then(async connection => {
//     const user = new User();
//     user.username = "Timber9";
//     user.age = 29;
//     user.password = '23134'
//     user.isDel = 0
//     user.createTime = new Date();

//     const userExtend = new UserExtend()
//     userExtend.mobile = '18015816566'
//     userExtend.address = '江苏南京'
//     //关联两个数据模型
//     userExtend.user = user
//     // connection.manager.save(user).then(user=>{
//     //     console.log(user);
//     // });
//     // const result = await connection.manager.save(user);
//     // console.log(result);
//     // 句柄方式
//     const userRepository = connection.getRepository(User)
//     // console.log('---------',userRepository);
//     await userRepository.save(user)
//     const userDetailRepository = connection.getRepository(UserExtend)
//    const result =  await userDetailRepository.save(userExtend)
//     console.log(result);
// }).catch(error => console.log(error));

 //查和删
createConnection().then(async connection => {
    //查和删
    //getRepository  句柄方式
    // const userRepository = connection.getRepository(User)
    // const user = await userRepository.findOne(2)
    // userRepository.remove(user).then(res=>{
    //     console.log('res',res);
    // })
    // 使用relations关联查询数据(正向查找)
    // const userRepository = connection.getRepository(User)
     // userDetail就是当前表中定义的字段
    // const result =await userRepository.find({relations:['userDetail']})
    // console.log(result);
    // 使用relations关联查询数据(反向查找)
    const userRepository = connection.getRepository(UserExtend)
     // user就是当前表中定义的字段
    const result =await userRepository.find({relations:['user']})
    console.log(result);
}).catch(error => console.log(error));

 //改
// createConnection().then(async connection => {
//     //查和删
//     //getRepository  句柄方式
//     const userRepository = connection.getRepository(User)
//     const user = await userRepository.findOne(3)
//     user.password = '117959'
//     const result = await userRepository.save(user)
//     console.log('result',result);
    
// }).catch(error => console.log(error));
